<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2022-10-25
 * Time: 20:24
 */

namespace App\controller\wechat;

use App\model\WechatApp;
use App\model\WechatMessage;
use support\Request;
use think\facade\Db;

class Message
{
    public function index(Request $request)
    {
        $wechat_app_id = $request->get('wechat_app_id', 2);
        $w = Db::table('wechat_message')->where('wechat_app_id', $wechat_app_id);
        if ($request->get('id')) {
            return json($w->where('id', $request->get('id'))->find());
        }
        $page =  $request->get('page', 1);
        $page_size =  $request->get('page_size', 10);
        return json($w->page($page, $page_size)->select()->toArray());
    }

    public function add(Request $request)
    {
        $wechat_app_id = $request->post('wechat_app_id', 2);
        $post=$request->post();
        $post['wechat_app_id']=$wechat_app_id;
        $res= WechatMessage::create($post);
        if($res){
            return json(['code' => 0, 'message' => '新增成功']);
        }else{
            return json(['code' => 0, 'message' => '新增失败']);
        }

    }
    public function edit(Request $request)
    {
        $post = $request->post();
        if(isset($post['id'])){
            $res=WechatMessage::update($post);
        }
        if ($res) {
            return json(['code' => 0, 'message' => '保存成功']);
        }else{
            return json(['code' => 403, 'message' => '保存失败']);
        }
    }
}